package com.ext.aic.ui;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.ext.aic.R;
import com.ext.aic.beans.IMUser;
import com.ext.aic.beans.MessageItem;
import com.ext.aic.databinding.AiTestLayoutBinding;
import com.ext.aic.holder.TextMessageViewHolder;
import com.eyinfo.android_pure_utils.snow.Butterfly;

import java.util.ArrayList;
import java.util.List;

import io.github.eyinfo.messages.commons.ImageLoader;
import io.github.eyinfo.messages.commons.models.IMessage;
import io.github.eyinfo.messages.messages.MsgListAdapter;

public class AITestActivity extends BaseActivity {

    private AiTestLayoutBinding binding;
    private MsgListAdapter<MessageItem> messageListAdapter;
    private List<MessageItem> messageList = new ArrayList<>();

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = View.inflate(this, R.layout.ai_test_layout, null);
        binding = AiTestLayoutBinding.bind(view);
        setContentView(binding.getRoot());
        bindView();
        initData();
    }

    private void bindView() {
        binding.topView.backIv.setOnClickListener(v -> finish());
        binding.topView.titleTv.setText(R.string.message_test);
        binding.sendBtn.setOnClickListener(this::onSendClick);
    }

    private void initData() {
        MsgListAdapter.HoldersConfig holdersConfig = new MsgListAdapter.HoldersConfig();
        holdersConfig.setSenderTxtMsg(TextMessageViewHolder.class, io.github.eyinfo.messages.R.layout.item_send_text);
        holdersConfig.setReceiverTxtMsg(TextMessageViewHolder.class, io.github.eyinfo.messages.R.layout.item_receive_txt);
        messageListAdapter = new MsgListAdapter<>("", holdersConfig, imageLoader);
        binding.rv.setAdapter(messageListAdapter);
        bindMessageEvent();
    }

    //region 消息数据
    IMUser receiveUser = new IMUser("0", "", "R.drawable.chat_receive_avatar");
    IMUser senderUser = new IMUser("1", "", "R.drawable.chat_send_avatar");

    private ImageLoader imageLoader = new ImageLoader() {

        @Override
        public void loadAvatarImage(ImageView imageView, String avatar) {
            int resId = getResources().getIdentifier(avatar.replace("R.drawable.", ""),
                    "drawable", getPackageName());
            imageView.setImageResource(resId);
        }

        @Override
        public void loadImage(ImageView imageView, String image) {

        }

        @Override
        public void loadVideo(ImageView imageView, String uri) {

        }
    };
    //endregion

    //region 消息元素事件
    private void bindMessageEvent() {
        messageListAdapter.setOnMsgClickListener(new MsgListAdapter.OnMsgClickListener<MessageItem>() {
            @Override
            public void onMessageClick(MessageItem message) {

            }
        });
        messageListAdapter.setMsgLongClickListener(new MsgListAdapter.OnMsgLongClickListener<MessageItem>() {
            @Override
            public void onMessageLongClick(View view, MessageItem message) {

            }
        });
        messageListAdapter.setOnAvatarClickListener(new MsgListAdapter.OnAvatarClickListener<MessageItem>() {

            @Override
            public void onAvatarClick(MessageItem message) {

            }
        });
        messageListAdapter.setMsgStatusViewClickListener(new MsgListAdapter.OnMsgStatusViewClickListener<MessageItem>() {
            @Override
            public void onStatusViewClick(MessageItem message) {

            }
        });
        messageListAdapter.setOnLoadMoreListener(new MsgListAdapter.OnLoadMoreListener() {
            @Override
            public void onLoadMore(int page, int totalCount) {

            }
        });
    }
    //endregion

    //region 发送消息
    private MessageItem buildSendMessage(String message) {
        MessageItem messageItem = new MessageItem();
        messageItem.setId(Butterfly.getInstance().nextId());
        messageItem.setSend(true);
        messageItem.setMessage(message);
        messageItem.setMMsgStatus(MessageItem.MessageStatus.SEND_GOING);
        messageItem.setType(IMessage.MessageType.SEND_TEXT.ordinal());
        messageItem.setUser(senderUser);
        return messageItem;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void onSendClick(View view) {
        String message = binding.contentEt.getText().toString();
        if (TextUtils.isEmpty(message)) {
            return;
        }
        MessageItem messageItem = buildSendMessage(message);
        messageList.add(messageItem);
        messageListAdapter.addToStart(messageItem, true);
        MessageItem receiveMessage = buildReceiveMessage("你好请问有什么可以帮到你的吗");
        messageList.add(receiveMessage);
        messageListAdapter.addToStart(receiveMessage, true);

        binding.contentEt.setText("");
    }
    //endregion

    //region 接收消息
    private MessageItem buildReceiveMessage(String message) {
        MessageItem messageItem = new MessageItem();
        messageItem.setId(Butterfly.getInstance().nextId());
        messageItem.setSend(false);
        messageItem.setMessage(message);
        messageItem.setMMsgStatus(MessageItem.MessageStatus.RECEIVE_GOING);
        messageItem.setType(IMessage.MessageType.RECEIVE_TEXT.ordinal());
        messageItem.setUser(receiveUser);
        return messageItem;
    }
    //endregion
}
